package jsf.uss.controllers;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import jsf.uss.data.IOrderRepository;
import jsf.uss.factory.HibernateUtil;
import jsf.uss.models.Order;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class OrderController implements IOrderRepository {

    public Order       order;
    public Session     session;
    public Transaction transaction;

    @Override
    public List<Order> getAll() {
	List<Order> ordersList = new ArrayList<Order>();
	this.session = HibernateUtil.getSessionFactory().openSession();
	this.transaction = session.beginTransaction();
	ordersList = castList(Order.class,
		this.session.createCriteria(Order.class).list());
	this.transaction.commit();

	this.session.flush();
	this.session.close();

	return ordersList;
    }

    @Override
    public Order getById(Long id) {
	this.session = HibernateUtil.getSessionFactory().openSession();
	this.transaction = session.beginTransaction();
	this.order = (Order) session.get(Order.class, id);
	this.transaction.commit();
	this.session.flush();
	this.session.close();

	return order;
    }

    @Override
    public boolean add(Order entity) {
	if (entity != null) {
	    this.session = HibernateUtil.getSessionFactory().openSession();
	    this.transaction = session.beginTransaction();
	    this.session.save(entity);
	    this.transaction.commit();
	    this.session.flush();
	    this.session.close();

	    return true;
	}
	else {
	    return false;
	}
    }

    @Override
    public boolean update(Order entity) {
	if (entity != null) {
	    this.session = HibernateUtil.getSessionFactory().openSession();
	    this.transaction = session.beginTransaction();
	    this.session.merge(entity);
	    this.transaction.commit();
	    this.session.flush();
	    this.session.close();

	    return true;
	}
	else {
	    return false;
	}
    }

    @Override
    public boolean remove(Long id) {
	Order order = (Order) session.get(Order.class, id);

	if (order != null) {
	    this.session = HibernateUtil.getSessionFactory().openSession();
	    this.transaction = session.beginTransaction();
	    this.session.delete(order);
	    this.transaction.commit();
	    this.session.flush();
	    this.session.close();

	    return true;
	}
	else {
	    return false;
	}
    }

    @Override
    public boolean remove(Order entity) {

	if (entity != null) {
	    this.session = HibernateUtil.getSessionFactory().openSession();
	    this.transaction = session.beginTransaction();
	    this.session.delete(entity);
	    this.transaction.commit();
	    this.session.flush();
	    this.session.close();

	    return true;
	}
	else {
	    return false;
	}
    }

    @Override
    public List<Order> getAllOrdersForClient(Long id) {
	this.session = HibernateUtil.getSessionFactory().openSession();
	this.session.beginTransaction();
	Query query = this.session
		.createQuery("select order from Order as order where order.client.clientId = :id");
	query.setParameter("id", id);
	List<Order> ordersForClient = castList(Order.class, query.list());
	this.session.getTransaction().commit();
	this.session.flush();
	this.session.close();

	return ordersForClient;
    }

    public static <T> List<T> castList(Class<? extends T> clazz, Collection<?> c) {
	List<T> r = new ArrayList<T>(c.size());
	for (Object o : c)
	    r.add(clazz.cast(o));
	return r;
    }
}
